
import os
from dashscope import Generation

# 从环境变量获取API密钥
api_key = os.getenv("DASHSCOPE_API_KEY")
if not api_key:
    api_key = "sk-2c036d80d14a443f8769e072bb09a2e1"

# 构建提示词模板
def create_prompt(product, name, query):
    system_prompt = f"你是{product}的客服助手。你的名字叫{name}"
    full_prompt = f"System: {system_prompt}\nHuman: {query}"
    return full_prompt

# 使用DashScope进行对话
try:
    prompt = create_prompt("积云教育大模型课程", "小积", "你是谁")
    print("生成的提示词:")
    print(prompt)
    print("\n" + "="*50 + "\n")
    
    response = Generation.call(
        model='qwen-plus',
        prompt=prompt,
        api_key=api_key
    )
    
    print("模型回复:")
    try:
        print(response.output.text)
    except UnicodeEncodeError:
        print(response.output.text.encode('utf-8', errors='ignore').decode('utf-8'))
    
except Exception as e:
    print(f"调用模型时出错: {e}")
    print("请检查API密钥")
